//
//  CustomerAPI.swift
//  Employee
//
//  Created by tancheng on 2018/7/23.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import Foundation
import Moya

let CustomerProvider = MoyaProvider<CustomerAPI> (
    
    manager: DefaultAlamofireManager.sharedManager

)


enum CustomerAPI {
    case getCustomerList(send: CustomerListSend)
    
    case getCustomerDetail(send: CustomerDetailSend)
    
    case getCustomerTagTree
    
    case updateCustomerTags(send: CustomerTagSend)
}


extension CustomerAPI: TargetType {
    
    public var baseURL: URL {
        return URL(string: GlobalConfig.BASE_OUT_URL)!
    }
    
    public var path: String {
        switch self {
        case .getCustomerDetail(send:):
            return "/ms/customer/detail.json"
        case .getCustomerList(send:):
            return "/ms/customer/house/list.json"
        case .getCustomerTagTree:
            return "/ms/tag/list/tree.json"
        case .updateCustomerTags(send:):
            return "/ms/customer/update/tag.json"
        }
    }
    
    var parameters: [String : Any]? {
        switch self {
        case let .getCustomerDetail(send):
            return ["access_token": Token.shared.token,"id": send.id]
        case let .getCustomerList(send):
            var params = [
                "access_token": Token.shared.token,
                "page.size":send.pageSize,
                "page.pn":send.pageIndex,
                ]
            if !send.keyword.isEmpty {
                params["search"] = send.keyword
            }
            return params
            
        case .getCustomerTagTree:
            return ["access_token": Token.shared.token]
        case let .updateCustomerTags(send):
                return ["access_token": Token.shared.token,"customerId": send.customerId,"tagIds":send.tagIds]
            
       }
    }
        
    var method:  Moya.Method {
        switch self {
        case .updateCustomerTags(send:):
            return .put
        default:
            return .get
        }
        
    }
    
    var sampleData: Data {
        return Data()
    }
    
    var task: Task {
        return  .requestParameters(parameters: self.parameters?.signParame(GlobalConfig.privateKey) ?? [:], encoding: URLEncoding.default)
    }
    
    var headers: [String : String]? {
         return GlobalConfig.httpHeaders
    }
    
}
